package org.example.controller;

import org.example.common.R;
import org.example.entity.User;
import org.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping
    public R add(@RequestBody User user){
        userService.save(user);
        return R.ok();
    }

    @PreAuthorize("hasAuthority('user:delete')")
    @DeleteMapping("/{roleId}")
    public R del(@PathVariable Long roleId){
        userService.removeById(roleId);
        return R.ok();
    }

    @PreAuthorize("hasAuthority('user:update')")
    @PutMapping
    public R update(@RequestBody User user){
        userService.saveOrUpdate(user);
        return R.ok();
    }

    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping
    public R getAll(){
        List<User> list =userService.list();
        return R.ok();
    }
}
